# tests/test_observe.py
import pytest
import pandas as pd
from observe import (
    calculate_overall_missing,
    calculate_missing_by_country,
    calculate_missing_by_year,
    plot_missing_data
)

@pytest.fixture
def sample_data():
    return pd.DataFrame({
        '国名Ch': ['中国', '美国', '中国', '美国'],
        'Year': [2020, 2020, 2021, 2021],
        'GDP': [14.7, 21.4, None, 23.0],
        '人口': [1400, 328, 1412, None]
    })

def test_calculate_overall_missing(sample_data):
    result = calculate_overall_missing(sample_data)
    assert 'GDP' in result
    assert result['GDP'] == 0.25

def test_calculate_missing_by_country(sample_data):
    result = calculate_missing_by_country(sample_data)
    assert result.loc['中国', 'GDP'] == 0.5
    assert result.loc['美国', '人口'] == 0.5

def test_calculate_missing_by_year(sample_data):
    result = calculate_missing_by_year(sample_data)
    assert 2020 in result.index
    assert 2021 in result.index

def test_plot_missing_data(sample_data):
    # 测试是否能成功生成图表而不报错
    fig = plot_missing_data(sample_data, 'GDP')
    assert fig is not None